home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / ana11 / graphics.c < prev    next >
C/C++ Source or Header  |  1993-01-15  |  14KB  |  446 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. #include "stdio.h"
  16. #include "ana.h"
  17. #include <X11/cursorfont.h>
  18. #include "graphics.h"
  19. #include "ana_glob.h"
  20.  
  21. #include "Bitmaps/gray"
  22. #include "Bitmaps/xpat"
  23. #include "Bitmaps/left_arrows"
  24. #include "Bitmaps/right_arrows"
  25. #include "Bitmaps/left_curs"
  26. #include "Bitmaps/left_mask"
  27. #include "Bitmaps/right_curs"
  28. #include "Bitmaps/right_mask"
  29. #include "Bitmaps/chk"
  30. #include "Bitmaps/iconbox"
  31. #include "Bitmaps/sizebox"
  32. #include "Bitmaps/select"
  33.  
  34.  
  35. public    PIX       pix;
  36. public    GCS       gcs;
  37. public    COL       colors;
  38. public    CURS      cursors;
  39.  
  40.  
  41. private    char   tops_bits[3][2] = { 0x5, 0x7, 0x1, 0x3, 0x4, 0x6 };
  42. private    char   bots_bits[3][2] = { 0x7, 0x5, 0x3, 0x1, 0x6, 0x4 };
  43.  
  44.  
  45. #define    SAME_COLOR( A, B )    \
  46.     ( (A).red == (B).red and (A).green == (B).green and (A).blue == (B).blue )
  47.  
  48. private int GetColor( key, colors, ndefined )
  49.   int     key;
  50.   XColor  colors[];
  51.   int     ndefined;
  52.   {
  53.     char      *s;
  54.     XColor    *color;
  55.     int       i;
  56.     Colormap  cmap = DefaultColormapOfScreen( screen );
  57.  
  58.     color = &colors[ndefined];
  59.     s = GetXDefault( key );
  60.     if( XParseColor( display, cmap, s, color ) == 0 )
  61.       {
  62.     if( IsDefault( key, s ) )
  63.         return( FALSE );
  64.  
  65.     fprintf( stderr, "server doesn't know color '%s'", s );
  66.     s = ProgDefault( key );
  67.     if( XParseColor( display, cmap, s, color ) == 0 )
  68.       {
  69.         fprintf( stderr, " or '%s'\n", s );        /* weird, should not be */
  70.         return( FALSE );
  71.       }
  72.     fprintf( stderr, "using '%s' instead\n", s );
  73.       }
  74.     for( i = 0; i < ndefined; i++ )
  75.       {
  76.     if( SAME_COLOR( *color, colors[i] ) )
  77.           {
  78.             color->pixel = colors[i].pixel;
  79.             return( TRUE );
  80.           }
  81.       }
  82.     return( (XAllocColor( display, cmap, color ) == 0) ? FALSE : TRUE );
  83.   }
  84.  
  85. #define    BACKGROUND_COLOR    0
  86. #define    FOREGROUND_COLOR    1
  87. #define    TRACE_COLOR        2
  88. #define    HIGHLIGHT_COLOR        3
  89. #define    BANNER_BG_COLOR        4
  90. #define    BANNER_FG_COLOR        5
  91. #define    BORDER_COLOR        6
  92. #define    MAX_NCOLORS        7
  93.  
  94. private void SetColors()
  95.   {
  96.     int       stat;
  97.     char      *def;
  98.     XColor    coldef[ MAX_NCOLORS ];
  99.  
  100.     if( PlanesOfScreen( screen ) < 2 )
  101.     goto mono;
  102.  
  103.     if( not GetColor( DEFL_BG, coldef, BACKGROUND_COLOR ) )
  104.     goto mono;
  105.     colors.black = coldef[ BACKGROUND_COLOR ].pixel;
  106.  
  107.     if( not GetColor( DEFL_FG, coldef, FOREGROUND_COLOR ) )
  108.     goto mono;
  109.     colors.white = coldef[ FOREGROUND_COLOR ].pixel;
  110.  
  111.     if( GetColor( DEFL_TRCOLOR, coldef, TRACE_COLOR ) )
  112.     colors.traces = coldef[ TRACE_COLOR ].pixel;
  113.     else
  114.     colors.traces = colors.white;
  115.  
  116.     if( GetColor( DEFL_HIGL, coldef, HIGHLIGHT_COLOR ) )
  117.       {
  118.     colors.color_hilite = TRUE;
  119.     colors.hilite = coldef[ HIGHLIGHT_COLOR ].pixel;
  120.     if( ((colors.black | colors.hilite) == colors.hilite and
  121.      (colors.traces & ~colors.hilite) == colors.traces ) )
  122.         colors.disj = 1;
  123.     else if( (colors.black & ~colors.hilite) == colors.hilite and
  124.      (colors.traces | colors.hilite) == colors.traces )
  125.         colors.disj = 2;
  126.     else
  127.         colors.disj = 0;
  128.       }
  129.     else
  130.       {
  131.     colors.disj = 0;
  132.     colors.hilite = colors.black;
  133.     colors.color_hilite = FALSE;
  134.     colors.mono = TRUE;
  135.       }
  136.  
  137.     if( GetColor( DEFL_BANN_BG, coldef, BANNER_BG_COLOR ) )
  138.     colors.banner_bg = coldef[ BANNER_BG_COLOR ].pixel;
  139.     else
  140.     colors.banner_bg = colors.white;
  141.  
  142.     if( GetColor( DEFL_BANN_FG, coldef, BANNER_FG_COLOR ) )
  143.     colors.banner_fg = coldef[ BANNER_FG_COLOR ].pixel;
  144.     else
  145.     colors.banner_fg = colors.black;
  146.  
  147.     if( GetColor( DEFL_BDRCOLOR, coldef, BORDER_COLOR ) )
  148.     colors.border = coldef[ BORDER_COLOR ].pixel;
  149.     else
  150.     colors.border = colors.black;
  151.  
  152.     return;
  153.  
  154.   mono:
  155.     colors.mono = TRUE;
  156.     colors.color_hilite = FALSE;
  157.     colors.disj = 0;
  158.     colors.black = BlackPixelOfScreen( screen );
  159.     colors.white = WhitePixelOfScreen( screen );
  160.     if( strcmp( GetXDefault( DEFL_RV ), "on" ) == 0 )
  161.     SWAP( Pixel, colors.black, colors.white );
  162.     colors.traces = colors.banner_bg = colors.white;
  163.     colors.hilite = colors.banner_fg = colors.border = colors.black;
  164.   }
  165.  
  166.  
  167. #define MakeBitmap( D, W, H )    \
  168.  XCreateBitmapFromData( display, DefaultRootWindow( display ), D, W, H );
  169.  
  170.  
  171. #define MakePixmap( D, W, H, FG, BG )    \
  172.  XCreatePixmapFromBitmapData( display, DefaultRootWindow( display ), \
  173.   D, W, H, FG, BG, DefaultDepthOfScreen( screen ) )
  174.  
  175.  
  176. private void InitBitmaps()
  177.   {
  178.     int     i;
  179.  
  180.     pix.gray = MakePixmap( gray_bits, gray_width, gray_height,
  181.      colors.white, colors.black );
  182.  
  183.     pix.xpat = MakePixmap( xpat_bits, xpat_width, xpat_height, colors.traces,
  184.      colors.black );
  185.  
  186.     pix.left_arrows = MakePixmap( left_arrows_bits, left_arrows_width,
  187.      left_arrows_height, colors.black, colors.white );
  188.  
  189.     pix.right_arrows = MakePixmap( right_arrows_bits, right_arrows_width,
  190.      right_arrows_height, colors.black, colors.white );
  191.  
  192.     for( i = 0; i < 3; i++ )
  193.       {
  194.     pix.tops[i] = MakePixmap( tops_bits[i], 3, 2,
  195.      colors.traces, colors.black);
  196.     pix.bots[i] = MakePixmap( bots_bits[i], 3, 2,
  197.      colors.traces, colors.black);
  198.       }
  199.  
  200.     pix.chk = MakePixmap( chk_bits, chk_width, chk_height, colors.black,
  201.      colors.white );
  202.  
  203.     pix.iconbox = MakePixmap( iconbox_bits, iconbox_width, iconbox_height,
  204.      colors.banner_fg, colors.banner_bg );
  205.  
  206.     pix.sizebox = MakePixmap( sizebox_bits, sizebox_width, sizebox_height,
  207.      colors.banner_fg, colors.banner_bg );
  208.  
  209.     pix.select = MakePixmap( select_bits, select_width, select_height,
  210.      colors.banner_fg, colors.banner_bg );
  211.  
  212.     pix.icon = InitIconBitmap();
  213.   }
  214.  
  215.  
  216. private Cursor MakeCursor( fg, bg, curs, mask, w, h, x, y )
  217.   XColor  *fg, *bg;
  218.   char    *curs, *mask;
  219.   int     w, h;
  220.   {
  221.     Pixmap  pcurs, pmask;
  222.     Cursor  cu;
  223.  
  224.     pcurs = MakeBitmap( curs, w, h );
  225.     pmask = MakeBitmap( mask, w, h );
  226.     cu = XCreatePixmapCursor( display, pcurs, pmask, fg, bg, x, y );
  227.     XFreePixmap( display, pcurs );
  228.     XFreePixmap( display, pmask );
  229.     return( cu );
  230.   }
  231.  
  232.  
  233. private void InitCursors()
  234.   {
  235.     XColor  cols[2];
  236.  
  237.     cols[0].pixel = colors.white;
  238.     cols[1].pixel = colors.black;
  239.     XQueryColors ( display, DefaultColormapOfScreen( screen ), cols, 2 );
  240. #define    WHITE    (&cols[0])
  241. #define    BLACK    (&cols[1])
  242.  
  243.     cursors.deflt = XCreateFontCursor( display, XC_left_ptr );
  244.     XRecolorCursor( display, cursors.deflt, WHITE, BLACK );
  245.  
  246.     cursors.left = MakeCursor( WHITE, BLACK, left_curs_bits, left_mask_bits,
  247.      left_curs_width, left_curs_height, left_curs_x_hot, left_curs_y_hot );
  248.  
  249.     cursors.right = MakeCursor( WHITE, BLACK, right_curs_bits,
  250.      right_mask_bits, right_curs_width, right_curs_height, right_curs_x_hot,
  251.      right_curs_y_hot );
  252.  
  253.     cursors.timer = XCreateFontCursor( display, XC_watch );
  254.     XRecolorCursor( display, cursors.timer, BLACK, WHITE );
  255.  
  256.     cursors.move = XCreateFontCursor( display, XC_fleur );
  257.     XRecolorCursor( display, cursors.timer, BLACK, WHITE );
  258. #undef    WHITE
  259. #undef    BLACK
  260.   }
  261.  
  262.  
  263. public void InitGraphics( font )
  264.   Font  font;
  265.   {
  266.     XGCValues      gcv;
  267.     unsigned long  mask;
  268.     Window         root;
  269.  
  270.     SetColors();
  271.     InitBitmaps();
  272.     InitCursors();
  273.     
  274.     root = RootWindowOfScreen( screen );
  275.  
  276.     mask = 0;
  277.     gcv.foreground = colors.white;            mask |= GCForeground;
  278.     gcv.background = colors.black;            mask |= GCBackground;
  279.     gcv.line_width = 1;                    mask |= GCLineWidth;
  280.     gcv.font = font;                    mask |= GCFont;
  281.     gcs.white = XCreateGC( display, root, mask, &gcv );
  282.  
  283.     mask = 0;
  284.     gcv.foreground = colors.black;            mask |= GCForeground;
  285.     gcv.background = colors.white;            mask |= GCBackground;
  286.     gcv.line_width = 1;                    mask |= GCLineWidth;
  287.     gcv.font = font;                    mask |= GCFont;
  288.     gcs.black = XCreateGC( display, root, mask, &gcv );
  289.  
  290.     mask = 0;
  291.     gcv.foreground = (colors.black | colors.white);    mask |= GCForeground;
  292.     gcv.function = GXinvert;                mask |= GCFunction;
  293.     gcv.plane_mask = (colors.black ^ colors.white);    mask |= GCPlaneMask;
  294.     gcv.line_width = 1;                    mask |= GCLineWidth;
  295.     gcs.inv = XCreateGC( display, root, mask, &gcv );
  296.  
  297.     mask = 0;
  298.     gcv.foreground = colors.white;            mask |= GCForeground;
  299.     gcv.background = colors.black;            mask |= GCBackground;
  300.     gcv.function = GXcopy;                mask |= GCFunction;
  301.     gcv.tile = pix.gray;                mask |= GCTile;
  302.     gcv.fill_style = FillTiled;                mask |= GCFillStyle;
  303.     gcs.gray = XCreateGC( display, root, mask, &gcv );
  304.  
  305.     if( colors.disj )
  306.       {
  307.     mask = 0;
  308.     gcv.foreground = colors.black;            mask |= GCForeground;
  309.     gcv.background = colors.traces;            mask |= GCBackground;
  310.     gcv.line_width = 1;                mask |= GCLineWidth;
  311.     gcv.font = font;                mask |= GCFont;
  312.     gcv.plane_mask = (colors.traces|colors.black);    mask |= GCPlaneMask;
  313.     gcs.traceBg = XCreateGC( display, root, mask, &gcv );
  314.  
  315.     mask = 0;
  316.     gcv.foreground = colors.traces;            mask |= GCForeground;
  317.     gcv.background = colors.black;            mask |= GCBackground;
  318.     gcv.line_width = 1;                mask |= GCLineWidth;
  319.     gcv.font = font;                mask |= GCFont;
  320.     gcv.plane_mask = (colors.traces|colors.black);    mask |= GCPlaneMask;
  321.     gcs.traceFg = XCreateGC( display, root, mask, &gcv );
  322.  
  323.     mask = 0;
  324.     gcv.foreground = colors.traces;            mask |= GCForeground;
  325.     gcv.background = colors.black;            mask |= GCBackground;
  326.     gcv.tile = pix.xpat;                mask |= GCTile;
  327.     gcv.fill_style = FillTiled;            mask |= GCFillStyle;
  328.     gcv.plane_mask = (colors.traces|colors.black);    mask |= GCPlaneMask;
  329.     gcs.xpat = XCreateGC( display, root, mask, &gcv );
  330.  
  331.     mask = 0;
  332.     gcv.foreground = colors.hilite;            mask |= GCForeground;
  333.     gcv.plane_mask = (~colors.traces & ~colors.black & colors.hilite);
  334.                             mask |= GCPlaneMask;
  335.     gcv.function = (colors.disj == 1) ? GXset : GXclear;
  336.                             mask |= GCFunction;
  337.     gcs.hilite = XCreateGC( display, root, mask, &gcv );
  338.  
  339.     mask = 0;
  340.     gcv.foreground = colors.hilite;            mask |= GCForeground;
  341.     gcv.plane_mask = (~colors.traces & ~colors.black & colors.hilite);
  342.                             mask |= GCPlaneMask;
  343.     gcv.function = (colors.disj == 2) ? GXset : GXclear;
  344.                             mask |= GCFunction;
  345.     gcs.unhilite = XCreateGC( display, root, mask, &gcv );
  346.     gcs.curs_on = gcs.hilite;
  347.     gcs.curs_off = gcs.unhilite;
  348.       }
  349.     else    /* colors not disjoint */
  350.       {
  351.     if( colors.traces == colors.white )
  352.         gcs.traceFg = gcs.white;
  353.     else
  354.       {
  355.         mask = 0;
  356.         gcv.foreground = colors.traces;        mask |= GCForeground;
  357.         gcv.background = colors.black;        mask |= GCBackground;
  358.         gcv.line_width = 1;                mask |= GCLineWidth;
  359.         gcv.font = font;                mask |= GCFont;
  360.         gcs.traceFg = XCreateGC( display, root, mask, &gcv );
  361.       }
  362.     gcs.traceBg = gcs.black;
  363.  
  364.     mask = 0;
  365.     gcv.foreground = colors.traces;            mask |= GCForeground;
  366.     gcv.background = colors.black;            mask |= GCBackground;
  367.     gcv.tile = pix.xpat;                mask |= GCTile;
  368.     gcv.fill_style = FillTiled;            mask |= GCFillStyle;
  369.     gcs.xpat = XCreateGC( display, root, mask, &gcv );
  370.  
  371.     if( colors.color_hilite )
  372.       {
  373.         mask = 0;
  374.         gcv.foreground = colors.hilite | colors.black;
  375.                             mask |= GCForeground;
  376.         gcv.plane_mask = colors.hilite ^ colors.black;
  377.                             mask |= GCPlaneMask;
  378.         gcv.function = GXinvert;            mask |= GCFunction;
  379.         gcv.line_width = 1;                mask |= GCLineWidth;
  380.         gcs.hilite = XCreateGC( display, root, mask, &gcv );
  381.         gcs.curs_off = gcs.curs_on = gcs.unhilite = gcs.hilite;  /* xor */
  382.       }
  383.     else
  384.       {
  385.         mask = 0;
  386.         gcv.function = GXinvert;            mask |= GCFunction;
  387.         gcv.fill_style = FillTiled;            mask |= GCFillStyle;
  388.         gcv.tile = pix.gray;            mask |= GCTile;
  389.         gcs.curs_on = XCreateGC( display, root, mask, &gcv );
  390.         gcs.curs_off = gcs.curs_on;
  391.         gcs.hilite = gcs.unhilite = gcs.inv;    /* use xor */
  392.       }
  393.       }
  394.  
  395.     if( colors.banner_bg == colors.white and
  396.       colors.banner_fg == colors.black )
  397.       {
  398.     gcs.bannerBg = gcs.white;
  399.     gcs.bannerFg = gcs.black;
  400.       }
  401.     else
  402.       {
  403.     mask = 0;
  404.     gcv.foreground = colors.banner_fg;        mask |= GCForeground;
  405.     gcv.background = colors.banner_bg;        mask |= GCBackground;
  406.     gcv.line_width = 1;                mask |= GCLineWidth;
  407.     gcv.font = font;                mask |= GCFont;
  408.     gcs.bannerFg = XCreateGC( display, root, mask, &gcv );
  409.  
  410.     mask = 0;
  411.     gcv.foreground = colors.banner_bg;        mask |= GCForeground;
  412.     gcv.background = colors.banner_fg;        mask |= GCBackground;
  413.     gcv.line_width = 1;                mask |= GCLineWidth;
  414.     gcv.font = font;                mask |= GCFont;
  415.     gcs.bannerBg = XCreateGC( display, root, mask, &gcv );
  416.       }
  417.  
  418.     mask = 0;
  419.     gcv.foreground = colors.banner_fg;            mask |= GCForeground;
  420.     gcv.background = colors.banner_bg;            mask |= GCBackground;
  421.     gcv.tile = pix.select;                mask |= GCTile;
  422.     gcv.fill_style = FillTiled;                mask |= GCFillStyle;
  423.     gcs.select = XCreateGC( display, root, mask, &gcv );
  424.  
  425.     if( colors.border == colors.black )
  426.     gcs.border = gcs.black;
  427.     else if( colors.border == colors.white )
  428.     gcs.border = gcs.white;
  429.     else
  430.       {
  431.     mask = 0;
  432.     gcv.foreground = colors.border;            mask |= GCForeground;
  433.     gcs.border = XCreateGC( display, root, mask, &gcv );
  434.       }
  435.  
  436.     mask = 0;
  437.     gcv.foreground = (colors.banner_fg | colors.banner_bg);
  438.                             mask |= GCForeground;
  439.     gcv.function = GXinvert;                mask |= GCFunction;
  440.     gcv.plane_mask = (colors.banner_fg ^ colors.banner_bg);
  441.                             mask |= GCPlaneMask;
  442.     gcv.line_width = 0;                    mask |= GCLineWidth;
  443.     gcv.subwindow_mode = IncludeInferiors;          mask |= GCSubwindowMode;
  444.     gcs.invert = XCreateGC( display, root, mask, &gcv );
  445.   }
  446.